/**
 * Created by Myron on 2015/8/21.
 */
//AngularJS 控制器
//日期单击
grApp.directive();
grApp.controller('SginDayAngularCtrl', ['$scope',
    '$http', 'SginDayFactory',
    function ($scope, $http, sginDayFactory) {

        var $modalconfirm =angular.element("#my-confirm");

        //变量初始化
        $scope.MonthSignDay; //月度签到天数
        $scope.AllSignDay; //累计签到天数

        $scope.GiftArray = [];
        $scope.load = function () {
            //页面获取数据
            sginDayFactory.getSginDayValue().then(function (result) {
                    $scope.MonthSignDay = result.MonthSignDay;
                    $scope.AllSignDay = result.AllSignDay;
                    $scope.SignState = result.SignState;
                    $scope.GiftArray = result.GiftArray;
                    //alert($scope.SignState);
            })
        };
        $scope.load();
        //签到按钮点击事件
        $scope.signBtn = function () {
            if ($scope.SignState == 0) {
                sginDayFactory.setSginDayAction().then(function (result) {
                    if (result.code == 0) {
                        //是否满足签到规则
                        console.log("签到成功，是否满足礼物领取条件"+result.hasGift);
                        if(result.hasGift==1){
                            console.log("签到成功，满足礼物领取条件"+result.level);
                            sginDayFactory.getGifts(result.level).then(function(data){
                                console.log("success");
                                AlertShow("提醒","签到成功,礼物已发放,请查收",true);
                            });
                        }else{
                            console.log("签到成功，不满足礼物领取条件");
                            AlertShow("提醒","签到成功",true);
                        }
                    } else {
                        if(result.code == 1011){
                            AlertShow("提醒","你今天已经签到过了哦!",false);
                        }else{
                            AlertShow("提醒","签到失败.页面正在维护中."+result.code,true);
                        }
                    }
                })
            }else{
                AlertShow("提醒","你今天已经签到过了哦!",false);
            }
        };
        $scope.giveGift = function(yearNumber,monthNumber,dayNumber){
            //将年月日传递到angular
            alert(yearNumber+"-"+monthNumber+"-"+dayNumber);
        };

        //alert模态框调用
        var AlertShow= function (title,content,reload) {
            var $modalalert =angular.element("#my-alert");
            $scope.AlertTitle=title;
            $scope.AlertContent=content;
            $scope.reload=reload;
            $modalalert.modal({
                relatedTarget: this,
                onCancel: function(e) {
                    if($scope.reload){
                        location.reload();
                    }
                }
            });
        }
    }]);

//签到工厂
grApp.factory('SginDayFactory', ['$http', '$q', function ($http, $q) {
    var service = {};
    //进入页面获取数据
    service.getSginDayValue = function () {
        var deferred = $q.defer(); // 声明延后执行，表示要去监控后面的执行
        $http({
            method: 'GET',
            url: '/activity/getSignDayValueAjax'
        }).success(function (data, status, headers, config) {
            deferred.resolve(data);
        }).
            error(function (data, status, headers, config) {
                deferred.reject(data);
            });
        return deferred.promise;
    };
    //签到Ajax操作
    service.setSginDayAction = function () {
        var deferred = $q.defer(); // 声明延后执行，表示要去监控后面的执行
        $http({
            method: 'GET',
            url: '/activity/setSignDayActionAjax'
        }).success(function (data, status, headers, config) {
            deferred.resolve(data);
        }).
            error(function (data, status, headers, config) {
                deferred.reject(data);
            });
        return deferred.promise;
    };
    //领取奖品操作
    service.getGifts=function(level){
        var deferred = $q.defer(); // 声明延后执行，表示要去监控后面的执行
        $http({
            method: 'GET',
            url: '/activity/getGift',
            params: {level: level}
        }).success(function (data, status, headers, config) {
            deferred.resolve(data);
        }).
            error(function (data, status, headers, config) {
                deferred.reject(data);
            });
        return deferred.promise;
    };
    return service;

}]);
